/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.grassbelt.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.longview.io.common.persistence.Page;
import com.longview.io.common.service.CrudService;
import com.longview.io.modules.grassbelt.entity.SysGrassBelt;
import com.longview.io.modules.grassbelt.dao.SysGrassBeltDao;
import com.longview.io.modules.grasscoverage.dao.GrassCoverageDao;
import com.longview.io.modules.grasscoverage.entity.GrassCoverage;

/**
 * 草原带管理Service
 * @author zf
 * @version 2018-04-17
 */
@Service
@Transactional(readOnly = true)
public class SysGrassBeltService extends CrudService<SysGrassBeltDao, SysGrassBelt> {
	@Autowired
	private GrassCoverageDao grassCoverageDao;
	public SysGrassBelt get(String id) {
		return super.get(id);
	}
	
	public SysGrassBelt getNearby(SysGrassBelt sysGrassBelt) {
		return dao.getNearby(sysGrassBelt);
	}
	
	public List<SysGrassBelt> findList(SysGrassBelt sysGrassBelt) {
		return super.findList(sysGrassBelt);
	}
	
	public Page<SysGrassBelt> findPage(Page<SysGrassBelt> page, SysGrassBelt sysGrassBelt) {
		return super.findPage(page, sysGrassBelt);
	}
	
	@Transactional(readOnly = false)
	public void save(SysGrassBelt sysGrassBelt) {
		super.save(sysGrassBelt);
		//插入盖度和产量
		for(int i=0;i<sysGrassBelt.getCoverage().size();i++)
		{
			GrassCoverage grasscoverage = new GrassCoverage();
			grasscoverage.setGrassbeltId(sysGrassBelt);
			grasscoverage.setId(sysGrassBelt.getCoverageids().get(i).toString());
			grasscoverage.setCoverage(sysGrassBelt.getCoverage().get(i).toString());
			grasscoverage.setProduction(sysGrassBelt.getProduction().get(i).toString());
			if (grasscoverage.getIsNewRecord()){
				grasscoverage.preInsert();
				grassCoverageDao.insert(grasscoverage);
			}else{
				grasscoverage.preUpdate();
				grassCoverageDao.update(grasscoverage);
			}			
		}

		
	}
	
	@Transactional(readOnly = false)
	public void delete(SysGrassBelt sysGrassBelt) {
		super.delete(sysGrassBelt);
	}
	
}